package com.levenx.utils.support;

import com.levenx.utils.HttpCallService;
import com.levenx.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Service
public class DefaultHttpCallService implements HttpCallService {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public <T> Result<T> get(String systemCode, String path, Class<T> responseType, Map<String, ?> uriVariables) {
        return this.get(systemCode+"/"+path,responseType,uriVariables);
    }

    @Override
    public <T> Result<T> get(String path, Class<T> responseType, Map<String, ?> uriVariables) {
        Object param = null != uriVariables ? uriVariables : new Object();
        return (Result) this.restTemplate.exchange(path, HttpMethod.GET,
                (HttpEntity)null,responseType,param).getBody();
    }
}
